<!DOCTYPE html>
<html lang="zh_CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>全选练习</title>
    <script>
        window.onload = function () {
            var checkedAllBox = document.getElementById("checkedAllBox");
            //全选
            var checkedAllBtn = document.getElementById("checkedAllBtn");
            checkedAllBtn.onclick = function () {
                var items = document.getElementsByName("items");
                for (var i = 0; i < items.length; i++) {
                    items[i].checked = true;
                };
                checkedAllBox.checked = true;
            };
            //全不选
            var checkedNoBtn = document.getElementById("checkedNoBtn");
            checkedNoBtn.onclick = function () {
                var items = document.getElementsByName("items");
                for (var i = 0; i < items.length; i++) {
                    items[i].checked = false;
                };
                checkedAllBox.checked = false;
            };
            //反选
            var checkedRevBtn = document.getElementById("checkedRevBtn");
            checkedRevBtn.onclick = function () {
                var items = document.getElementsByName("items");
                checkedAllBox.checked = true;
                for (var i = 0; i < items.length; i++) {
                    //第一种写法；
                    // if(items[i].checked) {
                    //     items[i].checked = false;
                    // }else {
                    //     items[i].checked = true;
                    // };
                    //第二种写法；
                    items[i].checked = !items[i].checked;
                    if (!items[i].checked) {
                            checkedAllBox.checked = false;
                        }
                };
            };
            //提交
            var sendBtn = document.getElementById("sendBtn");
            sendBtn.onclick = function () {
                var items = document.getElementsByName("items");
                for (var i = 0; i < items.length; i++) {
                    if (items[i].checked) {
                        alert(items[i].value);
                    };
                };
            };
            //全选/全不选
            checkedAllBox.onclick = function () {
                var items = document.getElementsByName("items");
                for (var i = 0; i < items.length; i++) {
                    items[i].checked = checkedAllBox.checked;
                };
            };
            //为四个单选框分别绑定单击响应函数
            var items = document.getElementsByName("items");
            for (var i = 0; i < items.length; i++) {
                items[i].onclick = function () {
                    checkedAllBox.checked = true;
                    for (var j = 0; j < items.length; j++) {
                        if (!items[j].checked) {
                            checkedAllBox.checked = false;
                            break;
                        }
                    };
                };
            };
        };
    </script>
</head>
<body>
    <form method="post" action="">
        你爱的运动时？<input type="checkbox" id="checkedAllBox" />全选/全不选
        <br />
        <input type="checkbox" name="items" value="足球" />足球
        <input type="checkbox" name="items" value="篮球" />篮球
        <input type="checkbox" name="items" value="羽毛球" />羽毛球
        <input type="checkbox" name="items" value="乒乓球" />乒乓球
        <br />
        <input type="button" id="checkedAllBtn" value="全 选" />
        <input type="button" id="checkedNoBtn" value="全不选" />
        <input type="button" id="checkedRevBtn" value="反选" />
        <input type="button" id="sendBtn" value="提 交" />
    </form>
</body>
</html>